package lab03.test6;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

public class Test6 {
    public static void main(String[] args) {
        String path = "D:\\hello.txt";
        RandomAccessFile randomAccessFile = null;
        int tem = 1;
        Map<String, Integer> map = new HashMap<String, Integer>();
        try {
            randomAccessFile = new RandomAccessFile(path, "r");
            String line = null;
            while ((line = randomAccessFile.readLine()) != null) {
                System.out.println(line);
                //判断MAP中是否有key
                if (map.get(line) == null) {
                    map.put(line, 1);
                } else {
                    int a = map.get(line);
                    map.put(line, a + 1);
                }
                tem = tem + 1;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("一共出现了"+tem+"个英文单词");
            System.out.println("有"+map.size()+"个互不相同的单词");
        }
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (Map.Entry<String, Integer> entry : map.entrySet()) {

                System.out.println("key= " + entry.getKey() + " and value= "
                        + entry.getValue());
            }
        }
    }

